package com.revenant.driver.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.Driver.EmptyCarManageItemAdapter;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.Driver.EmptyCarManageBean;
import com.revenant.driver.bean.Driver.SearchEmptyBean;
import com.revenant.driver.bean.ShipperBean.JieHuoCheLiangBean;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.revenant.driver.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

import static com.revenant.driver.utils.Constants.Driver_Car_Delete;
import static com.revenant.driver.utils.Constants.Driver_Car_Manage;
import static com.revenant.driver.utils.Constants.List_Empty_No_Authentication;

public class DriverEmptyCarManageActivity extends BaseActivity {

    @BindView(R.id.add_empety_car)
    TextView           addEmpetyCar;
    @BindView(R.id.jiehuocheliangrecy)
    RecyclerView       jiehuocheliangrecy;
    @BindView(R.id.jiehuocheliangsmart)
    SmartRefreshLayout jiehuocheliangsmart;
    private EmptyCarManageItemAdapter mItemAdapter;
    private Context                   context;

    private int pagernum = 1;

    @Override
    public int getContentViewResId() {
        context = this;
        return R.layout.activity_driver_emptycar_manage;
    }

    @Override
    public void initView() {
        setcenterTitle("空车管理");
        mItemAdapter = new EmptyCarManageItemAdapter();
        jiehuocheliangrecy.setLayoutManager(new LinearLayoutManager(context));
        jiehuocheliangrecy.setAdapter(mItemAdapter);
        jiehuocheliangsmart.autoRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        jiehuocheliangsmart.autoRefresh();
    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
    }

    @Override
    public void initData() {

        jiehuocheliangsmart.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                initDataRecyclerview();
                jiehuocheliangsmart.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        jiehuocheliangsmart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pagernum = 1;
                mItemAdapter.setNewData(null);
                initDataRecyclerview();
                jiehuocheliangsmart.finishRefresh(true);//刷新完成
            }
        });

        mItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("driverid", String.valueOf(mItemAdapter.getData().get(position).getId()));
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(context, DriverEmptyDetailActivity.class);
                startActivity(intent);

            }
        });

        mItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (Utils.isFastClick()){
                    if (view.getId() == R.id.delete_goods) {
                        new SweetAlertDialog(context, SweetAlertDialog.NORMAL_TYPE)
                                .setTitleText("确定删除吗")
                                .setCancelText("取消")
                                .setConfirmText("确定")
                                .showCancelButton(true)
                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        getGoodsDetail(String.valueOf(mItemAdapter.getData().get(position).getId()), position,sweetAlertDialog);
                                    }
                                })
                                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sDialog) {
                                        sDialog.dismissWithAnimation();
                                    }
                                })
                                .show();

                    }
                }
            }
        });
    }

    private void initDataRecyclerview() {

        OkGo.<String>get(Driver_Car_Manage)
                .params("driverId", SPUtils.getAccounId(context))
                .params("isAuthentication", "1")
                .params("pageNum", pagernum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        EmptyCarManageBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), EmptyCarManageBean.class);
                        int pageNum = shipperPublishedBean.getData().getPageNum();
                        int pages = shipperPublishedBean.getData().getPages();
                        if (shipperPublishedBean.getCode().equals("0")) {
                            if (pageNum == 1) {
                                mItemAdapter.setNewData(shipperPublishedBean.getData().getResult());
                            } else if (pageNum <= pages) {
                                mItemAdapter.addData(shipperPublishedBean.getData().getResult());
                            } else {
                                mItemAdapter.loadMoreEnd();
                            }

                        } else {
                            mItemAdapter.loadMoreFail();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.add_empety_car)
    public void onViewClicked() {
        startActivity(DriverPublishEmptyActivity.class);
    }

    private void getGoodsDetail(String dataid, int pos,SweetAlertDialog sweetAlertDialog) {
        OkGo.<String>post(Driver_Car_Delete)

                .params("id", dataid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        SearchEmptyBean g = GsonUtil.parseJsonWithGson(response.body(), SearchEmptyBean.class);
                        if (g.getCode().equals("0")) {
                            sweetAlertDialog.dismissWithAnimation();
                            showToast(g.getMsg());
                            mItemAdapter.remove(pos);
                        }

                    }
                });
    }

}
